//
//  YWTailorView.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/8/21.
//

#import "YWTailorView.h"
#import "YWTailorRangeView.h"

@interface YWTailorView ()<YWTailorRangeViewDelegate>


@property(nonatomic,strong)UIImageView *ogrinalImageView; //原始图片view

@property(nonatomic,strong)UIImageView *coverImageView; //遮盖半透明view

@property(nonatomic,strong)UIImageView *showImageView; //显示裁剪内容和半透明view

@property(nonatomic,strong)YWTailorRangeView *tailorView; //负责手势裁剪view

@property (nonatomic, strong) UIImage * ogrinalImage;

@property (nonatomic, strong) UIImage * coverImage;

@property(nonatomic,assign)CGRect tailorFrame;

@end

@implementation YWTailorView

//1、四层view都是铺满drawView的size、第四层里还有一层控制裁剪大小范围
//2、起始范围是上下左右都是15
//3、控制的是rangeView的上下左右的constraint
//4、8个imageView和1个view有手势

-(YWTailorRangeView *)tailorView{
    if(_tailorView == nil){
        YWTailorRangeView *tailorView = [YWTailorRangeView tailorRangeView];
        tailorView.delegate = self;
        _tailorView = tailorView;
    }
    return _tailorView;
}

-(UIImageView *)ogrinalImageView{
    if(_ogrinalImageView == nil){
        UIImageView *ogrinalImageView = [[UIImageView alloc] init];
        _ogrinalImageView = ogrinalImageView;
    }
    return _ogrinalImageView;
}


-(UIImageView *)coverImageView{
    if(_coverImageView == nil){
        UIImageView *coverImageView = [[UIImageView alloc] init];
        _coverImageView = coverImageView;
    }
    return _coverImageView;
}

-(UIImageView *)showImageView{
    if(_showImageView == nil){
        UIImageView *showImageView = [[UIImageView alloc] init];
        _showImageView = showImageView;
    }
    return _showImageView;
}

//纯代码用这个
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

//xib用这个
-(void)awakeFromNib{
    [super awakeFromNib];
    [self setup];
}

//初始化
-(void)setup{
    [self addSubview:self.ogrinalImageView];
//    [self addSubview:self.coverImageView];
    [self addSubview:self.showImageView];
    [self addSubview:self.tailorView];
    self.clipsToBounds = YES;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self initTailor];
    });
    
}


-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.ogrinalImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.coverImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.showImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.tailorView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}

-(void)setOgrinalImage:(UIImage *)image{
    _ogrinalImage = image;
    
    self.ogrinalImageView.image = image;
}

- (void)setCoverImage:(UIImage *)image{
    
    _coverImage = image;
    
    self.coverImageView.image = image;
    
}

-(void)initTailor{
    
    //赋值可活动范围
    self.tailorView.dealSize = self.dealSize;
    
    //确定擦除区域
    CGFloat x = 15;
    CGFloat y = 15;
    CGFloat w = self.dealSize.width - 2*x;
    CGFloat h = self.dealSize.height - 2*y;
    CGRect rect = CGRectMake(x, y, w, h);
    
    self.tailorFrame = rect;
    
    //生成一张带有透明擦除区域的图片
    //1.开启图片上下文
    UIGraphicsBeginImageContextWithOptions(self.dealSize, NO, 0);
    //2.把UIImageV内容渲染到当前的上下文当中
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.coverImageView.layer renderInContext:ctx];
    //3.擦除上下文当中的指定的区域
    CGContextClearRect(ctx, rect);
    //4.从上下文当中取出图片
    UIImage *newImage =  UIGraphicsGetImageFromCurrentImageContext();
    
    //替换之前ImageView的图片
    self.showImageView.image = newImage;
    
    //5.关闭上下文
    UIGraphicsEndImageContext();
}

-(void)tailorRangeView:(YWTailorRangeView *)tailorRangeView tailorRange:(CGRect)rect{
    
    self.tailorFrame = rect;
    
    //生成一张带有透明擦除区域的图片
    //1.开启图片上下文
    UIGraphicsBeginImageContextWithOptions(self.dealSize, NO, 0);
    //2.把UIImageV内容渲染到当前的上下文当中
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.coverImageView.layer renderInContext:ctx];
    //3.擦除上下文当中的指定的区域
    CGContextClearRect(ctx, rect);
    //4.从上下文当中取出图片
    UIImage *newImage =  UIGraphicsGetImageFromCurrentImageContext();
    
    //替换之前ImageView的图片
    self.showImageView.image = newImage;
    
    //5.关闭上下文
    UIGraphicsEndImageContext();
}

//获取当前图层
-(UIImage *)getCurrentLayer{
    
    //self.tailorFrame.size是确定了你要生成图片的大小
    UIGraphicsBeginImageContextWithOptions(self.tailorFrame.size, NO, 0);
    
    //裁剪区域就是裁剪的大小、所以是CGRectMake(0, 0, self.tailorFrame.size.width, self.tailorFrame.size.height)
    //把ImageV绘制到上下文之前,设置一个裁剪区域
    CGRect rect = CGRectMake(0, 0, self.tailorFrame.size.width, self.tailorFrame.size.height);
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithRect:rect];
    [clipPath addClip];
    
    NSLog(@"self.tailorFrame:%@",NSStringFromCGRect(self.tailorFrame));
    
    //把当前的ImageView渲染到上下文当中
//    CGContextRef ctx =  UIGraphicsGetCurrentContext();
//    [self.ogrinalImageView.layer renderInContext:ctx];
    
    //不能用上面的renderInContext、只能用drawAtPoint、且x和y是负数确定位置
    CGFloat tailorX = -self.tailorFrame.origin.x;
    CGFloat tailorY = -self.tailorFrame.origin.y;
    CGPoint tailorPoint = CGPointMake(tailorX, tailorY);
    [self.ogrinalImage drawAtPoint:tailorPoint];
    
    //.从上下文当中生成 一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    
    return newImage;
    
}

@end
